package com.module.third.wx;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;

import com.module.third.R;

import java.io.ByteArrayOutputStream;

/**
 * @author bsnl_yanxuwen
 * @date 2021/8/2 16:08
 * Description :
 */
class WxCommonUtils {
    /**
     * 裁剪
     *
     * @param bitmap 原图
     * @return 裁剪后的图像
     */
    static Bitmap cropBitmap(Bitmap bitmap) {
        int w = bitmap.getWidth(); // 得到图片的宽，高
        int h = bitmap.getHeight();
        if (w == h) {
            return bitmap;
        }
        if (w > h) {
            //横
            return Bitmap.createBitmap(bitmap, (w / 2) - (h / 2), 0, h, h, null, false);
        } else {
            //竖
            return Bitmap.createBitmap(bitmap, 0, (h / 2) - (w / 2), w, w, null, false);
        }
    }

    /**
     * 设置缩略图
     *
     * @param bitMap
     * @param needRecycle
     * @return
     */
    public static Bitmap createBitmapThumbnail(Bitmap bitMap, boolean needRecycle) {
        if (bitMap != null) {
            int width = bitMap.getWidth();
            int height = bitMap.getHeight();
            // 设置想要的大小
            int newWidth = 100;
            int newHeight = 100;
            // 计算缩放比例
            float scaleWidth = ((float) newWidth) / width;
            float scaleHeight = ((float) newHeight) / height;
            // 取得想要缩放的matrix参数
            Matrix matrix = new Matrix();
            matrix.postScale(scaleWidth, scaleHeight);
            // 得到新的图片
            Bitmap newBitMap = Bitmap.createBitmap(bitMap, 0, 0, width, height,
                matrix, true);
            if (needRecycle) bitMap.recycle();
            return newBitMap;
        }
        return null;
    }

    /**
     * Bitmap转换成byte[]并且进行压缩,压缩到不大于maxkb
     *
     * @param bitmap
     * @param
     * @return
     */
    public static byte[] bitmapBytes(int flag, Context context, Bitmap bitmap, int maxkb) {
        if (flag == 1) {
            bitmap = drawableBitmapOnWhiteBg(context, bitmap);
        }

        ByteArrayOutputStream output = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 80, output);
        int options = 100;
        while (output.toByteArray().length > maxkb && options != 10) {
            output.reset(); //清空output
            bitmap.compress(Bitmap.CompressFormat.PNG, options, output);//这里压缩options%，把压缩后的数据存放到output中
            options -= 10;
        }


        return output.toByteArray();
    }

    //背景图变色白色
    public static Bitmap drawableBitmapOnWhiteBg(Context context, Bitmap bitmap) {
        Bitmap newBitmap = null;
        try {
            newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(newBitmap);
            canvas.drawColor(context.getResources().getColor(android.R.color.white));
            Paint paint = new Paint();
            canvas.drawBitmap(bitmap, 0, 0, paint); //将原图使用给定的画笔画到画布上
        } catch (Exception e) {
            newBitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.app_icon_logo);
        }
        return newBitmap;
    }


    public static String buildTransaction(final String type) {
        return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
    }
}
